package com.warm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.warm.core.page.TableDataInfo;
import com.warm.core.utils.StringUtils;
import com.warm.core.utils.file.MimeTypeUtils;
import com.warm.orm.domain.sys.SysHotPlug;
import com.warm.orm.domain.sys.dto.SysHotPlugDto;
import com.warm.orm.domain.sys.vo.SysHotPlugVo;
import com.warm.system.api.orm.agent.SysHotPlugAgent;
import com.warm.system.service.ISysHotPlugService;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.handle.UploadedFile;

import java.io.IOException;
import java.util.List;

import static com.warm.core.utils.file.FileUploadUtils.hotPlugUploadBaseDir;
import static com.warm.core.utils.file.FileUploadUtils.upload;


/**
 * 扩展插件Service业务层处理
 *
 * @author xian.zi.ming@qq.com
 * @date 2025-02-28
 */
@Component
public class SysHotPlugServiceImpl implements ISysHotPlugService {

    @Inject
    private SysHotPlugAgent sysHotPlugAgent;

    @Override
    public SysHotPlugVo selectById(String qualifiedName) {
        return sysHotPlugAgent.selectById(qualifiedName);
    }

    @Override
    public TableDataInfo<SysHotPlugVo> selectPage(SysHotPlugDto dto) {
        return sysHotPlugAgent.selectPage(dto);
    }

    @Override
    public List<SysHotPlugVo> selectList(SysHotPlugDto dto) {
        return sysHotPlugAgent.selectList(dto);
    }

    public LambdaQueryWrapper<SysHotPlug> buildQuery(SysHotPlugDto dto) {
        return new LambdaQueryWrapper<SysHotPlug>()
                .like(StringUtils.isNotBlank(dto.getAuthor()), SysHotPlug::getAuthor, dto.getAuthor())
                .like(StringUtils.isNotBlank(dto.getWebsite()), SysHotPlug::getWebsite, dto.getWebsite())
                .like(StringUtils.isNotBlank(dto.getJarName()), SysHotPlug::getJarName, dto.getJarName())
                .eq(StringUtils.isNotBlank(dto.getStatus()), SysHotPlug::getStatus, dto.getStatus());
    }

    @Override
    public int insertByDto(SysHotPlugDto dto) {
        return sysHotPlugAgent.insertByDto(dto);
    }

    @Override
    public int updateByDto(SysHotPlugDto dto) {
        return sysHotPlugAgent.updateByDto(dto);
    }

    @Override
    public int deleteByIds(String[] qualifiedNames) {
        return sysHotPlugAgent.deleteByIds(qualifiedNames);
    }

    @Override
    public boolean checkDictTypeUnique(SysHotPlugDto dto) {
        return sysHotPlugAgent.checkDictTypeUnique(dto);
    }

    @Override
    public String uploadPlug(UploadedFile file) throws IOException {
        try {
            return upload(hotPlugUploadBaseDir, file, MimeTypeUtils.JAR_EXTENSION);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
}
